package com.wang.users.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.service.BaseService;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAMenuDao;
import com.wang.users.dao.IARoleMenuDao;
import com.wang.users.dao.impl.AMenuDaoImpl;
import com.wang.users.dao.impl.ARoleMenuDaoImpl;
import com.wang.users.pojo.AMenu;
import com.wang.users.pojo.ARole;
import com.wang.users.pojo.ARoleMenu;
import com.wang.users.service.IAuthDbService;
import com.wang.users.service.IUsersDbService;

/**
 * 权限相关的Service类
 * @author jjh
 *
 */
public class AuthDbServiceImpl extends BaseService implements IAuthDbService
{
	private IUsersDbService usersDbService; 
	
	public void setUsersDbService(IUsersDbService usersDbService)
	{
		this.usersDbService = usersDbService;
	}
	
	/*
	 * 菜单dao的实现类
	 * 菜单角色dao的实现类
	 */
	private IAMenuDao menuDao = new AMenuDaoImpl();
	private IARoleMenuDao roleMenuDao = new ARoleMenuDaoImpl();
	/*=========菜单表操作开始===========*/
	@Override
	public JSONObject saveOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		/* 调用dao保存到数据库 */
		int res = this.menuDao.saveOne(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneMenuService(AMenu menu)
	{
		JSONObject resultJson = new JSONObject();
		menu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.menuDao.updateOne(menu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), menu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.menuDao.deleteOne(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public AMenu findOneMenuService(Map<String, Object> condMap)
	{
		AMenu menu =  this.menuDao.findOne(condMap);
		if(menu != null)
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			if("true".equalsIgnoreCase(condMap.get("extend") + ""))
			{
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				AMenu parentMenu = this.findOneMenuService(extMap);
				menu.setParentMenu(parentMenu);

				/* 查询孩子 */
				extMap.clear();
				extMap.put("parentId", menu.getId());
				List<AMenu> childrenList = this.findCondListMenuService(null, extMap);
				menu.setChildMenuList(childrenList);
				
				/* 查询角色菜单 */
				extMap.put("menuId", menu.getId());
				List<ARoleMenu> roleMenuList = this.findCondListRoleMenuService(null, condMap);
				menu.setRoleMenuList(roleMenuList);
			}
		}
		return menu ; 
	}

	@Override
	public List<AMenu> findCondListMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AMenu> list =  this.menuDao.findCondList(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			for (AMenu menu : list)
			{
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				AMenu parentMenu = this.findOneMenuService(extMap);
				menu.setParentMenu(parentMenu);

				/* 查询孩子 */
				extMap.clear();
				extMap.put("id", menu.getParentId());
				List<AMenu> childrenList = this.findCondListMenuService(null, extMap);
				menu.setChildMenuList(childrenList);
			}
		}
		return list ;
	}
	/*=========菜单表操作结束===========*/ 
	
	/*=========菜单角色表操作开始===========*/ 
	@Override
	public JSONObject saveOneRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setCreateTime(new Date());
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.saveOne(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRoleMenuService(ARoleMenu roleMenu)
	{
		JSONObject resultJson = new JSONObject();
		roleMenu.setUpdateTime(new Date());
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.updateOne(roleMenu);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_ID.getInfo(), roleMenu.getId());
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneRoleMenuService(Map<String, Object> condMap)
	{
		JSONObject resultJson = new JSONObject();
		/* 调用dao保存数据库 */
		int res = this.roleMenuDao.deleteOne(condMap);
		if(res > 0 )
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_SUCCESS.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_SUCCESS.getCode())));
			
			JSONObject dataJson = new JSONObject();
			dataJson.put(InfoEnum.VAR_EFFECT.getInfo(), res);
			
			resultJson.put(InfoEnum.VAR_DATA.getInfo(), dataJson);
		}else
		{
			resultJson.put(InfoEnum.VAR_CODE.getInfo(), InfoEnum.INFO_FAILED.getCode());
			resultJson.put(InfoEnum.VAR_INFO.getInfo(), ConstatFinalUtil.INFO_JSON.get(String.valueOf(InfoEnum.INFO_FAILED.getCode())));
		}
		return resultJson;
	}

	@Override
	public ARoleMenu findOneRoleMenuService(Map<String, Object> condMap)
	{
		ARoleMenu roleMenu = this.roleMenuDao.findOne(condMap);
		Map<String,Object> extMap = new HashMap<String, Object>();
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系;
			 * 它是一棵树形结果 */
			
			/* 查询父类 */
			extMap.clear();
			extMap.put("id", roleMenu.getMenuId());
			AMenu menu = this.findOneMenuService(extMap);
			roleMenu.setMenu(menu);

			/* 查询孩子 */
			extMap.clear();
			extMap.put("id", roleMenu.getRoleId());
			ARole role = this.usersDbService.findOneRoleService(extMap);
			roleMenu.setRole(role);
		}
		return roleMenu;
	}

	@Override
	public List<ARoleMenu> findCondListRoleMenuService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ARoleMenu> list = this.roleMenuDao.findCondList(pageInfoUtil, condMap);
		Map<String,Object> extMap = new HashMap<String, Object>();
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			for (ARoleMenu roleMenu : list)
			{
				/* 查询关联关系;
				 * 它是一棵树形结果 */
				
				/* 查询父类 */
				extMap.clear();
				extMap.put("id", roleMenu.getMenuId());
				AMenu menu = this.findOneMenuService(extMap);
				roleMenu.setMenu(menu);
				
				/* 查询孩子 */
				extMap.clear();
				extMap.put("id", roleMenu.getRoleId());
				ARole role = this.usersDbService.findOneRoleService(extMap);
				roleMenu.setRole(role);
			}
		}
		return list;
	}
	/*=========菜单角色表操作结束===========*/

	@Override
	public List<AMenu> findCondListTreeMenuService(int id, String operType)
	{
		List<AMenu> resultList = new ArrayList<AMenu>();
		if(id == 0 )
		{
			/* 如果id为0,查询所有的一级节点 */
			Map<String,Object> condMap = new HashMap<String, Object>();
			condMap.put("parentId", id);
			List<AMenu> rootList = this.findCondListMenuService(null, condMap);
			for (AMenu menu : rootList)
			{
				/* 向下找 */
				this.findCondListTreeMenuDownServiceUtil(menu.getId(), 0 , resultList);
			}
		}else
		{
			if("up".equalsIgnoreCase(operType))
			{
				/* 向上找得先确定id */
				this.findCondListTreeMenuUpServiceUtil(id, 0, resultList);
				/* 反转 */
				Collections.reverse(resultList);
			}else
			{
				/* 向下找 */
				this.findCondListTreeMenuDownServiceUtil(id, 0 , resultList);
			}
		}
		return resultList;
	} 
	
	/**
	 * 向上找
	 * @param id
	 * @param operType
	 * @param level
	 * @param resultList
	 */
	private void findCondListTreeMenuUpServiceUtil(int id, int level,List<AMenu> resultList)
	{
		/* 根据id查询当前结点 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		condMap.put("extend", "true");
		AMenu menu = this.findOneMenuService(condMap);
		if(menu != null)
		{
			//System.out.println("==循环==" + menu.getName());
			/* 这句话很关键是为了存储结果 */
			resultList.add(menu);
			/* 再往下讨论 */
			AMenu menuTemp = menu.getParentMenu();
			if(menuTemp != null)
			{
				findCondListTreeMenuUpServiceUtil(menuTemp.getId(), level + 1,resultList);
			}
		}
	}

	/**
	 * 树形结构的工具方法（递归）（向下找）
	 * @param id
	 * @param operType
	 */
	private void findCondListTreeMenuDownServiceUtil(int id, int level,List<AMenu> resultList)
	{
		/* 根据id查询当前结点 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		condMap.put("extend", "true");
		AMenu menu = this.findOneMenuService(condMap);
		if(menu != null)
		{
			StringBuffer sb = new StringBuffer();
			for(int i = 0 ; i < level; i ++ )
			{
				sb.append("|-->");
			}
			//System.out.println("==循环==" + sb.toString() + menu.getName());
			menu.setTreeName(sb.toString() + menu.getName());
			/* 这句话很关键是为了存储结果 */
			resultList.add(menu);
			/* 再往下讨论 */
			List<AMenu> childMenuList = menu.getChildMenuList() ;
			for (AMenu menuTemp : childMenuList)
			{
				findCondListTreeMenuDownServiceUtil(menuTemp.getId(), level + 1,resultList);
			}
		}
	}
}
